const mysql = require("mysql");

const pool = mysql.createPool({
    host: "127.0.0.1",
    user: "root",
    password: 'root',
    database: "zhongjiunet"
})

const exec = (sql, params) => {
    return new Promise((resolve, reject) => {
        pool.getConnection((err, conn) => {
            if (err) {
                console.log("连接数据库里失败", err.message);
                reject(err.message);
            }
            conn.query(sql, params, function (err, result) {
                if (err) {
                    console.log("执行sql语句失败", err.message);
                    reject(err.message);
                }
                resolve(result);
                //释放链接
                conn.release();
            })
        })
    })
}

//暴露方法
module.exports = {
    exec
}
